<template>
  <!-- 筛选区 -->
  <div class="sub-filter">
    <div class="item" v-for="item in filterList" :key="item.id">
      <div class="head">{{ item.name }}：</div>
      <div class="body" ref="BODY_LIST">
        <a href="javascript:;">全部</a>
        <a
          href="javascript:;"
          v-for="sec_item in item.properties"
          :key="sec_item.id"
          >{{ sec_item.name }}</a
        >
      </div>
    </div>
  </div>
</template>

<script setup name="SubFilter">
import { computed, inject, onMounted, ref } from "vue";
const tempData = inject("filterList");
const filterList = computed(() => {
  return [
    { id: "brands", name: "品牌", properties: tempData.value.brands },
    ...tempData.value.saleProperties
  ];
});

// 获取Body，筛选列表可见元素
const BODY_LIST = ref();
onMounted(() => {
  const domArr = Array.from(BODY_LIST.value);
  domArr.forEach((item) => {
    const LastVisibleDom = [];
    if (item.offsetHeight === 120) {
      for (let i = 0; i < item.children.length; i++) {
        if (item.children[i].offsetTop <= 80) {
          LastVisibleDom.push(item.children[i]);
        }
      }
    }
    if (LastVisibleDom.length && LastVisibleDom.length < item.children.length) {
      const PARENT_NODE = item;
      const HEIGHT = LastVisibleDom.at(-1).offsetHeight;
      const WIDTH = LastVisibleDom.at(-1).offsetWidth;
      const newItem = document.createElement("a");
      newItem.id = 'more'
      newItem.style.display = 'inline-block'
      newItem.style.color = '#27BA9B'
      newItem.style.marginRight = WIDTH + 36 + 'px'
      newItem.style.height = HEIGHT + 'px'
      newItem.style.fontWeight = 'bold'
      newItem.href = 'javascript:;'
      newItem.innerText = "更多";
      newItem.onclick = () => {
        PARENT_NODE.style['max-height'] = '999px'
        PARENT_NODE.removeChild(newItem)
      }
      PARENT_NODE.insertBefore(newItem, LastVisibleDom.at(-1));
    }
  });
});
</script>

<style scoped lang="less">
// 筛选区
.sub-filter {
  background: #fff;
  padding: 25px;
  .item {
    display: flex;
    line-height: 40px;
    .head {
      width: 80px;
      color: #999;
    }
    .body {
      flex: 1;
      transition: max-height 0.5s;
      max-height: 120px;
      overflow: hidden;
      position: relative;
      // contain: paint;
      a {
        margin-right: 36px;
        transition: all 0.3s;
        display: inline-block;
        &.active,
        &:hover {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
